local duliang = fk.CreateSkill {

  name = "fk__duliang",

  tags = {  },

}

local U = require "packages/utility/utility"


duliang:addEffect("active", {
  anim_type = "support",
  card_num = 0,
  target_num = 1,
  prompt = "#fk__duliang",
  can_use = function(self, player)
    return player:usedSkillTimes(duliang.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    local flag = (to_select == player) and "ej" or "hej"
    return #selected == 0 and #to_select:getCardIds(flag) > 0
  end,
  on_use = function(self, room, effect)
    local player =effect.from
    local target =effect.tos[1]
    local flag = (target == player) and "ej" or "hej"
    local get = room:askForCardChosen(player, target, flag, duliang.name)
    room:obtainCard(player, get, false, fk.ReasonPrey)
    if player.dead or target.dead then return end
    local choice = room:askForChoice(player, {"duliang_put", "duliang_draw"}, duliang.name)
    if choice == "duliang_put" then
      local put = room:askForCard(player, 1, 999, false, duliang.name, true, ".", "#fk__duliang-put")
      if #put == 0 then return end
      if #put > 1 then
        put = room:askForGuanxing(player, put, nil, {0,0}, duliang.name, true).top
      end
      room:moveCards({
        ids = table.reverse(put),
        from = player.id,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = duliang.name,
        proposer = player.id,
      })
      if not target.dead then
        local cards = room:getNCards(#put + 1)
        U.viewCards(target, cards, duliang.name)
        local types = {}
        for _, id in ipairs(put) do
          table.insertIfNeed(types, Fk:getCardById(id).type)
        end
        for i = #cards, 1, -1 do
          local id = cards[i]
          if not table.contains(types, Fk:getCardById(id).type) then
            table.remove(cards, i)
          end
        end
        if #cards > 0 then
          room:moveCardTo(cards, Card.PlayerHand, target, fk.ReasonPrey, duliang.name, "", false)
        end
        if not player.dead then
          player:drawCards(1, duliang.name)
        end
      end
    else
      room:addPlayerMark(target, "@duliang", 1)
    end
  end,
})

duliang:addEffect(fk.DrawNCards, {
  name = "#fk__duliang_trigger",
  mute = true,
  events = {fk.DrawNCards},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@duliang") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.n = data.n + player:getMark("@duliang")
    player.room:setPlayerMark(player, "@duliang", 0)
  end,
})

return duliang